package com.example.administrator.himalaya.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.himalaya.R;
import com.example.administrator.himalaya.callback.OnclickListner;
import com.example.administrator.himalaya.entity.categoryentity.BooksEntity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
    private OnclickListner listner;
    private Context context;
    private List<BooksEntity> list;
    private ViewHolder viewHolder;

    public CategoryAdapter(Context context, OnclickListner listner, List<BooksEntity> list) {
        this.context = context;
        this.listner = listner;
        this.list = list;
        list = new ArrayList<>();
    }

    @Override
    public int getItemViewType(int position) {
        if (position ==0){
            return R.layout.sort_header;
        }else {
            return  R.layout.sort_item;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        viewHolder = new ViewHolder(LayoutInflater.from(context).inflate(viewType, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position==0){

            Picasso.with(context).load(list.get(position).getCoverPath())
                    .into(holder.image);
        }else {
            holder.textView.setText(list.get(position).getTitle());
            Picasso.with(context).load(list.get(position).getCoverPath())
                    .into(holder.imageView);
        }

    }

    public void addAll(Collection<? extends BooksEntity> collection) {
        int size = list.size();
        list.addAll(collection);
        //添加一组数据
        notifyItemRangeInserted(size, collection.size());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        private ImageView imageView;
        private ImageView image;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv_sort_item_books);
            imageView = (ImageView) itemView.findViewById(R.id.image_sort_item);
            image = (ImageView) itemView.findViewById(R.id.iv_sort_header);
        }
    }
}

